package com.lagou.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020/11/29.
 *
 * @author chouFy
 */
@Configuration
public class ElasticsearchConfig {


    @Value("${lagouelasticsearch.elasticsearch.hostlist}")
    private String hostList;


    @Bean
    public RestHighLevelClient RestHighLevelClient(){
        // 解析host 信息
        String[] hosts = hostList.split(",");
        HttpHost[] httpHosts = new HttpHost[hosts.length];
        for (int i = 0; i < hosts.length; i++) {
            String items = hosts[i];
            httpHosts[i] = new HttpHost(items.split(":")[0] ,
                    Integer.parseInt(items.split(":")[1]) , "http");

        }
        return new RestHighLevelClient(RestClient.builder(httpHosts));
    }
}
